/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.core.common.utils.SpringCommonTest
   Module Description   :

   Date Created      : 2008/12/24
   Original Author   : jeffma
   Team              : Yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.core.common.utils;

import org.hibernate.SessionFactory;
import org.junit.Ignore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;

import com.yaodian100.core.admin.entity.User;

/**
 * @author jeffma
 * 
 */
@Ignore
public class SpringCommonTest {

	/** logger */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static SessionFactory sessionFactory;
	protected static ClassPathXmlApplicationContext ctx = null;

	public static void configCtx() {
		try {
			ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml", "applicationContext-*.xml" });
			String username = System.getenv("USERNAME");
			if (StringUtils.isBlank(username)) {
				username = System.getenv("USER");
			}
			if (StringUtils.isBlank(username)) {
				username = "test00";
			}
			if (ctx != null && ctx.getBean("sessionFactory") != null) {
				sessionFactory = (SessionFactory) ctx.getBean("sessionFactory");
			}
			User tester = new User(username);
			SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(tester, "password"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// @Before
	// public void setUpMethod() throws Exception {
	// if (sessionFactory != null) {
	// Session session = SessionFactoryUtils.getSession(sessionFactory, true);
	// TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
	// }
	// }
	//
	// @After
	// public void tearDownMethod() throws Exception {
	// if (sessionFactory != null) {
	// SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
	// SessionFactoryUtils.closeSession(sessionHolder.getSession());
	// }
	// }
}
